package com.example.controller;

import com.example.pojo.Emp;
import com.example.pojo.Result;
import com.example.service.EmpService;
import com.example.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_DaChiZi
 * Date: 2025/9/5
 * Time: 9:56
 */
@RestController
@Slf4j
public class LoginController {
    @Autowired
    EmpService empService;

    /*
    * 员工登录功能
    * */
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        Emp e =  empService.login(emp);
        if (e != null) {
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getUsername());

            String str =  JwtUtils.generateJwt(claims);
            return Result.success(str);
        } else {
            return Result.error("用户名或密码错误");
        }

    }
}
